use crate::xueliuhz::constant::MJFanXing::*;
use crate::xueliuhz::fan::*;
use crate::xueliuhz::*;
use crossterm::event::Event;
use log::info;
use mahjong_lib::*;
use std::any::Any;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::collections::HashMap;
use tge::{algorithm::remove_nv, context::Context, event::event_emit, game::Model, util::Rand};

pub const TILEW: usize = 7;
pub const TILEH: usize = 5;

// enum MJState {
//     Normal,
//     OverSelf,
//     OverBorder,
// }

pub struct MJModel {
    pub mjt: MJTiles,
    pub pool: Vec<u8>,
}

impl MJModel {
    pub fn new() -> Self {
        Self {
            mjt: MJTiles::new(),
            pool: vec![],
        }
    }

    pub fn shuffle_tiles(&mut self, rd: &mut Rand) {
        self.pool.clear();
        //WAN，TONG，TIAO三门，为了容易胡取一门
        for i in 0..2u8 {
            for j in 0..9u8 {
                for _k in 0..4u8 {
                    self.pool.push(j + 1 + i * 10);
                }
            }
        }
        for _i in 0..4 {
            self.pool.push(35);
        }
        rd.shuffle(&mut self.pool);
    }

    pub fn test_card(&mut self) {
        let vc = vec![
            CPG {
                cpg_type: CPGType::CHI,
                gang_type: GangType::AN,
                tile: 3,
                com: [3, 4, 5, 0],
                from_seat: 0,
            },
            CPG {
                cpg_type: CPGType::PENG,
                gang_type: GangType::AN,
                tile: 3,
                com: [0, 0, 0, 0],
                from_seat: 0,
            },
            CPG {
                cpg_type: CPGType::GANG,
                gang_type: GangType::AN,
                tile: 3,
                com: [0, 0, 0, 0],
                from_seat: 0,
            },
        ];
        let mut csx = XlhzTiles::new();
        let mut csx_c = XlhzTiles::new();
        let vhand: Vec<u8> = vec![1, 1, 1, 2, 2, 2, 2, 4, 4, 5, 5, 6, 6, 35];
        let mut aok;
        aok = csx_c.assign(&vhand, 0, TileType::WAN, &vc, HuType::UNKNOWN);
        info!("aok error....{:?}", aok);
        aok = csx.assign(&vhand, 0, TileType::WAN, &vec![], HuType::UNKNOWN);
        info!("csx.hand_now...{:?}", csx.ti_hand_now);

        info!("aok....{:?} {:?}", aok, fx_qi_dui(&mut csx));

        let mut tv = Vec::<u8>::new();
        csx.ti_hand.get_hands(&mut tv, 0);
        info!("tv...gethands..0:{:?}", tv);
        tv.clear();
        csx.ti_hand.get_hands(&mut tv, 1);
        info!("tv...gethands..1:{:?}", tv);


        let xfx = XlhzCalFx {
            hongzhong_count: 4,
            hu_result: HashMap::new(),
            csx_raw: csx,
            csx_cal: csx_c,
        };

        let mut s = DefaultHasher::new();
        xfx.hash(&mut s);
        info!("hash of xfx...{}", s.finish());
        info!("test fanxing...{:?}", QINGYISE as u16);

        let mut vc = vec![2u8, 3, 4, 2];
        // let mut vc = vec![2u8];
        let rv = remove_nv(&mut vc, 2, 2);
        info!("after remove_nv...{:?},rv={}", vc, rv);
    }

    //pub fn act(&mut self, _d: Dir, _context: &mut Context) {
    //}
}

impl Model for MJModel {
    fn init(&mut self, ctx: &mut Context) {
        ctx.rand.srand_now();
        let lz: Vec<u8> = vec![35];
        for _i in 0..500000000 {
            self.shuffle_tiles(&mut ctx.rand);
            info!("{:?}", &self.pool[0..14]);
            self.mjt.assign(&self.pool[0..14], &lz).unwrap();
            if self.mjt.is_hu() {
                info!("HUHUHU{:?}", &self.pool[0..14]);
                break;
            }
        }
        self.test_card();
        event_emit("MJ.RedrawTile");
    }

    fn handle_input(&mut self, context: &mut Context, _dt: f32) {
        let es = context.input_events.clone();
        for e in &es {
            match e {
                Event::Key(_key) => {}
                _ => {}
            }
        }
        context.input_events.clear();
    }

    fn handle_auto(&mut self, _context: &mut Context, _dt: f32) {}
    fn handle_event(&mut self, _context: &mut Context, _dt: f32) {}
    fn handle_timer(&mut self, _context: &mut Context, _dt: f32) {}

    fn as_any(&mut self) -> &mut dyn Any {
        self
    }
}

